package com.apotik.legenda.frame;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;

import javax.swing.JFrame;

import com.apotik.legenda.dialog.ClosePOSDialog;
import com.apotik.legenda.model.Shift;
import com.apotik.legenda.model.User;
import com.apotik.legenda.observer.GeneralObserver;
import com.apotik.legenda.panel.PosPanel;


/**
 * @author Makhsus Bilmajdi
 */
public class PosFrame implements GeneralObserver {

	private JFrame frame;
	private JFrame mainFrame;
	
	private ClosePOSDialog confirmDialog;
	private User userCashier;
	private Shift shift;
	
	public PosFrame(JFrame mainFrame, User selectedCashier, Shift shift) {
		this.mainFrame = mainFrame;
		this.userCashier = selectedCashier;
		this.shift = shift;
		
		frame = new JFrame();
		frame.setTitle("POS");
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		frame.setBounds(10, 10, 1200, 700);

		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				formWindowClosing(evt);
			}
		});
		
		frame.getContentPane().add(new PosPanel(selectedCashier, shift).getSplitPane());
	}

	// show mainFrame when closing this frame and then dispose this frame
	private void formWindowClosing(WindowEvent evt) {		
		boolean isVisible = false;
		if(confirmDialog!=null){
			isVisible = confirmDialog.isVisible();
		}
		
		if(isVisible){
			confirmDialog.toFront();
			confirmDialog.requestFocus();
		}else{
			confirmDialog = new ClosePOSDialog(this, this.userCashier, this.shift);
			confirmDialog.setVisible(true);
			confirmDialog.requestFocus();
		}
	}
	
	public JFrame getFrame() {
		return frame;
	}

	@Override
	public void response(HashMap<String, Object> hm) {
		mainFrame.setVisible(true);
		frame.dispose();
	}
	
}
